package me.feng.analyzeAS
{
	import mx.utils.StringUtil;

	/**
	 * As文件
	 * @author feng 2016-3-31
	 */
	public class ASFile
	{
		public var path:String;
		private var _content:String;

		public function get saveTsPath():String
		{
			return QClassName.split(".").join("\\").split("::").join("\\") + ".ts";
		}

		public function get content():String
		{
			return _content;
		}

		public function set content(value:String):void
		{
			_content = value;
		}

		public function ASFile(content:String = null)
		{
			this.content = content;
		}

		/**
		 * 初始化
		 * @param content	AS文件文本内容
		 */
		public function init(content:String):void
		{
			_content = content;
		}

		public function get packageName():String
		{
			var reg:RegExp = KeyWordRegExp.PACKAGE_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			var str:String = StringUtil.trim(result[1]);
			return str;
		}

		public function get className():String
		{
			var reg:RegExp = KeyWordRegExp.CLASS_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			if (result == null)
				return null;
			var str:String = StringUtil.trim(result[1]);
			return str;
		}

		public function get allClassName():Array
		{
			var reg:RegExp = KeyWordRegExp.ALL_CLASS_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			var arr:Array = [];
			while (result != null)
			{
				var str:String = StringUtil.trim(result[1]);

				arr.push(str);
				result = reg.exec(_content);
			}
			return arr;
		}



		public function get interfaceName():String
		{
			var reg:RegExp = KeyWordRegExp.INTERFACE_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			if (result == null)
				return null;
			var str:String = StringUtil.trim(result[1]);
			return str;
		}


		public function get superClass():String
		{
			var reg:RegExp = KeyWordRegExp.SUPER_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			if (result == null)
				return null;
			var str:String = StringUtil.trim(result[1]);
			return str;
		}

		/**
		 * 继承的接口列表
		 */
		public function get useInterfaces():Array
		{
			var reg:RegExp = KeyWordRegExp.USE_INTERFACE_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);
			if (result == null)
				return null;
			var arr:Array = result[1].split(",");
			for (var i:int = 0; i < arr.length; i++)
			{
				arr[i] = StringUtil.trim(arr[i]);
			}

			return arr;
		}

		private var _funcitons:Vector.<ASFunctionInfo>;

		public function get functions():Vector.<ASFunctionInfo>
		{
			if (_funcitons != null)
				return _funcitons;
			var reg:RegExp = KeyWordRegExp.FUNCTION_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);

			_funcitons = new Vector.<ASFunctionInfo>();
			while (result != null)
			{
				var asFunctionInfo:ASFunctionInfo = new ASFunctionInfo(result);
				asFunctionInfo.block = getBlock(reg.lastIndex);
				_funcitons.push(asFunctionInfo);
				result = reg.exec(_content);
			}

			return _funcitons;
		}

		private var _accessors:Vector.<ASAccessorInfo>;

		/**
		 * 存取器列表 get set
		 */
		public function get accessors():Vector.<ASAccessorInfo>
		{
			if (_accessors != null)
				return _accessors;

			var reg:RegExp = KeyWordRegExp.ACCESSORS_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);

			_accessors = new Vector.<ASAccessorInfo>();
			while (result != null)
			{
				var asAccessorInfo:ASAccessorInfo = new ASAccessorInfo(result)
				asAccessorInfo.block = getBlock(reg.lastIndex);
				_accessors.push(asAccessorInfo);
				result = reg.exec(_content);
			}

			return _accessors;
		}

		private var _attributes:Vector.<ASVariableDescribe>;

		/**
		 * 属性列表
		 */
		public function get attributes():Vector.<ASVariableDescribe>
		{
			if (_attributes != null)
				return _attributes;

			var reg:RegExp = KeyWordRegExp.ATTRIBUTE_RE;
			reg.lastIndex = 0;
			var result:* = reg.exec(_content);

			_attributes = new Vector.<ASVariableDescribe>;
			while (result != null)
			{
				var variableDescribe:ASVariableDescribe = new ASVariableDescribe();
				variableDescribe.initVisit(result[1]);

				variableDescribe.name = result[5];
				variableDescribe.type = result[7];

				_attributes.push(variableDescribe);

				result = reg.exec(_content);
			}

			return _attributes;
		}

		/**
		 * 获取代码块
		 * @param startIndex
		 * @return
		 */
		private function getBlock(startIndex:int):String
		{
			var startIndex1:int = _content.indexOf(KeyWords.BLOCK_START, startIndex);

			var blockValue:int = 0;
			for (var i:int = startIndex1; i < _content.length; i++)
			{
				if (_content.charAt(i) == KeyWords.BLOCK_START)
					blockValue = blockValue + 1;
				if (_content.charAt(i) == KeyWords.BLOCK_END)
					blockValue = blockValue - 1;
				if (blockValue == 0)
					break;
			}
			return _content.substring(startIndex, i + 1);
		}

		private var _QClassName:String;

		public function get QClassName():String
		{
			if (_QClassName != null)
				return _QClassName;

			var strArr:Array = [];
			if (packageName != null && packageName.length > 0)
			{
				strArr.push(packageName);
			}
			if (className != null && className.length > 0)
			{
				strArr.push(className);
			}
			else if (interfaceName != null && interfaceName.length > 0)
			{
				strArr.push(interfaceName);
			}
			else
			{
				strArr.push(functions[0].name);
			}
			_QClassName = strArr.join("::");
			return _QClassName;
		}
	}
}
